/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            main.c
* version:         1.0
* created_by:      Carlos Calvillo
* date_created:    May 17 2010
*=============================================================================*/
/* DESCRIPTION : Source file to implement main function.                      */
/*============================================================================*/
/* FUNCTION COMMENT : Implements entry point (main function) and other        */
/* application stuff.                                                         */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 17/may/2010 |                               | Carlos Calvillo  */
/* Initial version for the Smart Car Race 2010 contest using Freescale        */
/* Bolero micro.                                                              */
/*============================================================================*/

/* Includes */
/* -------- */
#include "stdtypedef.h"
#include "sysinit.h"
#include "ioapi.h"
#include "beMIOS.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                 */
/*==================================================*/ 
/* BYTE constants */

/* WORD constants */

/* LONG and STRUCTURE constants */

/*======================================================*/ 
/* Definition of RAM variables                 */
/*======================================================*/ 
/* BYTE RAM variables */
T_UBYTE raub_BufferPinPE1[2];		/* Array used to check PIN transitions */
T_UBYTE raub_BufferPinPE2[2];		/* Array used to check PIN transitions */
T_UBYTE raub_BufferPinPE3[2];		/* Array used to check PIN transitions */

/* WORD RAM variables */

/* LONG and STRUCTURE RAM variables */

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/* Private functions prototypes */
/* ---------------------------- */
T_UBYTE main_chkPinTransition(T_UBYTE lub_pinName, PTR_UBYTE lpub_pinBuff, T_UBYTE lub_transTo1);

/* Exported functions prototypes */
/* ----------------------------- */

/* Inline functions */
/* ---------------- */

/* Private functions */
/* ----------------- */

/**************************************************************
 *  Name                 : 	main
 *  Description          : 	main function is usually the entry point for application.
 *  Parameters           : 	none
 *  Return               : 	int
 *  Critical/explanation : 	none.
 **************************************************************/
int main(void) 
{
	/* ======================================== */
  	/*              INITIALIZATIONS             */
  	/* ======================================== */
  	
	/* Mode initializations */
	/* -------------------- */
	sysinit_InitMode();
	
	/* Clock initializations */
	/* --------------------- */
	sysinit_InitSysClock();
	
	/* Configure Pins */
	/* -------------- */
	/* Pin PE3 will be used as GPIO-input to increment PWM duty cycle */
	IO_SetPinFctn(pin_PE3,PA_GPIO);
	IO_SetPinAsInput(pin_PE3);
	/* Pin PE2 will be used as GPIO-input to decrement PWM duty cycle */
	IO_SetPinFctn(pin_PE2,PA_GPIO);
	IO_SetPinAsInput(pin_PE2);
	/* Pin PE1 will be used as GPIO-input to set PWM duty cycle to 50% */
	IO_SetPinFctn(pin_PE1,PA_GPIO);
	IO_SetPinAsInput(pin_PE1);
	
	/* PWM initialization */
	/* ------------------ */
	/* Init eMIOS 0 */	
	beMIOS_initEMIOS0();
	/* Init eMIOS 0 channel 23 as a OPWMB */	
	beMIOS_initEMIOS0_ch23();
	/* Associate PIN PE7 to the eMIOS 0 channel 23 function */
	IO_SetPinFctn(pin_PE7,PA_AM1);	
	
	/* Loop forever */
	
	for (;;) 
	{
		/* INCREASE PWM OUTPUT */
		/* ------------------- */
		if(main_chkPinTransition(pin_PE3,raub_BufferPinPE3,1))
		{
			/* Increment current Duty Cycle in 5% */
			beMIOS0_setPWMBdutyCycle(23,PWM_DC_INC,5);	
		}
		
		/* DECREASE PWM OUTPUT */
		/* ------------------- */
		if(main_chkPinTransition(pin_PE2,raub_BufferPinPE2,1))
		{
			/* Decrement current Duty Cycle in 5% */
			beMIOS0_setPWMBdutyCycle(23,PWM_DC_DEC,5);	
		}
		
		/* SET PWM TO 50% */
		/* -------------- */
		if(main_chkPinTransition(pin_PE1,raub_BufferPinPE1,1))
		{
			/* Set current Duty Cycle to 50% */
			beMIOS0_setPWMBdutyCycle(23,PWM_DC_SET,50);	
		}
	}
}

/**************************************************************
 *  Name                 :  main_chkPinTransition
 *  Description          :  Check for PIN transitions.
 *  Parameters           :  [input]	lub_pinName -> Pin to check for.
 *							[input/output] lpub_pinBuff -> Pointer to a variable storing last and new read PIN values..
 *							[input] lub_trans -> if transition to the value indicated by this parameter occurs
 *									this function returns TRUE, otherwise it returns FALSE. 
 *  Return               :  TRUE if transition on selected PIN to the selected value is detected.
 *							FALSE if transition has not ocurred.
 *  Critical/explanation :  No
 **************************************************************/
T_UBYTE main_chkPinTransition(T_UBYTE lub_pinName, PTR_UBYTE lpub_pinBuff, T_UBYTE lub_trans)
{
	T_UBYTE lub_retVal;
	
	/* Initial return value */
	lub_retVal = FALSE;
	
	/* Push new Pin value into buffer */
	/* ------------------------------ */
	/* Shift old Pin value */
	lpub_pinBuff[0] = lpub_pinBuff[1];
	/* Insert new Pin Value */
	lpub_pinBuff[1] = IO_PinRead(lub_pinName);
	/* Check for transition */
	/* -------------------- */			
	/* If transition to Press occurred on PIN PE1, call the beMIOS0_setPWMBdutyCycle function */
	if((lpub_pinBuff[0] != lpub_pinBuff[1]) && (lpub_pinBuff[1] == lub_trans))
	{
		/* Transition to the desired value on lub_transTo1 has occurred */	
		lub_retVal = TRUE;	
	}
	
	return(lub_retVal);	
}

/* Exported functions */
/* ------------------ */


